package hu.dobrosi.dinkycam.j2me.forms;

import hu.dobrosi.dinkycam.j2me.DinkycamApp;
import hu.dobrosi.dinkycam.j2me.Labels;
import hu.dobrosi.dinkycam.j2me.util.Preferences;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.rms.RecordStoreException;

public class AdvancedSettingsForm extends DinkycamForm implements
		CommandListener {

	private Command cmdBack;
	private Preferences preferences;
	private DecimalTextField gpsMovingSleepTimeTextField;
	private DecimalTextField gpsStandbySleepTimeTextField;
	private DecimalTextField gpsStandbySamplingTimeTextField;
	private DecimalTextField gpsLimitTimeTextField;

	public AdvancedSettingsForm(DinkycamApp app, String title) {
		super(app, title);
	}

	public void buildGUI() {
		super.buildGUI();
		setTitle(Labels.TITLE_ADVANCED_SETTINGS.toString());
		gpsMovingSleepTimeTextField = new DecimalTextField(
				Labels.GPS_MOVING_SLEEP_TIME.toString(), "0", 6);// sec
		gpsStandbySleepTimeTextField = new DecimalTextField(
				Labels.GPS_STANDBY_SLEEP_TIME.toString(), "60", 4);// min
		gpsStandbySamplingTimeTextField = new DecimalTextField(
				Labels.GPS_STANDBY_SAMPLING_TIME.toString(), "60", 3);// sec
		gpsLimitTimeTextField = new DecimalTextField(
				Labels.GPS_LIMIT_TIME.toString(), "5", 2);// min
		append(gpsMovingSleepTimeTextField);
		append(gpsStandbySleepTimeTextField);
		append(gpsStandbySamplingTimeTextField);
		append(gpsLimitTimeTextField);
		setCommandListener(this);

		preferences = DinkycamApp.getPreferences();
		preferences.loadTextFieldValue(this, gpsMovingSleepTimeTextField);
		preferences.loadTextFieldValue(this, gpsStandbySleepTimeTextField);
		preferences.loadTextFieldValue(this, gpsStandbySamplingTimeTextField);
		preferences.loadTextFieldValue(this, gpsLimitTimeTextField);
		addCommand(cmdBack = new Command(Labels.BACK.toString(), Command.BACK,
				1));
	}

	public void commandAction(Command c, Displayable s) {
		preferences.saveTextFieldValue(this, gpsMovingSleepTimeTextField);
		preferences.saveTextFieldValue(this, gpsStandbySleepTimeTextField);
		preferences.saveTextFieldValue(this, gpsStandbySamplingTimeTextField);
		preferences.saveTextFieldValue(this, gpsLimitTimeTextField);
		try {
			preferences.save();
		} catch (RecordStoreException e) {
			app.showError(this, "commandAction", e);
		}
		if (c.equals(cmdBack)) {
			app.showSettings();
		}
		super.commandAction(c, s);
	}

	/**
	 * default value 0 sec
	 * 
	 * @return the gpsMovingSleepTimeTextField
	 */
	public DecimalTextField getGpsMovingSleepTimeTextField() {
		return gpsMovingSleepTimeTextField;
	}

	/**
	 * default value 60 min
	 * 
	 * @return the gpsStandbySleepTimeTextField
	 */
	public DecimalTextField getGpsStandbySleepTimeTextField() {
		return gpsStandbySleepTimeTextField;
	}

	/**
	 * default value 60 sec
	 * 
	 * @return the gpsStandbySamplingTimeTextField
	 */
	public DecimalTextField getGpsStandbySamplingTimeTextField() {
		return gpsStandbySamplingTimeTextField;
	}

	/**
	 * default value 5 min
	 * 
	 * @return the gpsLimitTimeTextField
	 */
	public DecimalTextField getGpsLimitTimeTextField() {
		return gpsLimitTimeTextField;
	}

}